-- 新机制：弹药
local ammo = fk.CreateSkill {
    name = "mcdh_ammo",
}

local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

ammo:addEffect(fk.DrawNCards,{
    global = true,
    priority = 0.1,
    can_trigger = function(self, event, target, player, data)
        return target == player and data.n > 0 and player:getMark("mcdh_ammo_max") >0 and player:getMark("mcdh_ammo_max")-player:getMark("mcdh_ammo")>0
    end,
    on_cost = function(self, event, target, player, data)
        return player.room:askForSkillInvoke(player, self.name, data, "#ammo-Invoke")
    end,
    on_use = function(self, event, target, player, data)
        local room =player.room
        local choices = {}
        for i = 1, math.min(data.n,player:getMark("mcdh_ammo_max")-player:getMark("mcdh_ammo")), 1 do
            table.insert(choices, tostring(i))
        end
        local choice = room:askForChoice(target, choices, self.name)
        lib.AmmoAndCharge({type = "ammo",num = tonumber(choice) ,skil_name = self.name,method="Load",from = player})
        data.n = data.n - tonumber(choice)
    end,
})

--初始化弹药
ammo:addEffect(fk.AfterPropertyChange,{
    global = true,
    can_refresh = function(self, event, target, player, data)
        local general = Fk.generals[player.general]
        local to = data.from
        return data.general and data.general ~= "" and data.results.generalChange and player == to
    end,
    on_refresh = function(self, event, target, player, data)
        local room = player.room
        local general = Fk.generals[player.general]
        if general.maxAmmo then
            local now_max_ammo = player:getMark("mcdh_ammo_max")
            lib.mcdh_ammo(player, 0,general.maxAmmo-now_max_ammo )
        else
            room:setPlayerMark(player, "mcdh_ammo_max",0)
            room:setPlayerMark(player, "mcdh_ammo", 0)
            room:setPlayerMark(player, "@mcdh_ammo", 0)
        end
        if general.ammo then
            local now_ammo = player:getMark("mcdh_ammo")
            lib.mcdh_ammo(player, general.ammo-now_ammo)
        end
    end,
})

ammo:addEffect(fk.GamePrepared,{
    global = true,
    can_refresh = function(self, event, target, player, data)
        return true
    end,
    on_refresh = function(self, event, target, player, data)
        local room = player.room
        local general = Fk.generals[player.general]
        if general.maxAmmo then
            local now_max_ammo = player:getMark("mcdh_ammo_max")
            lib.mcdh_ammo(player, 0,general.maxAmmo-now_max_ammo )
        else
            room:setPlayerMark(player, "mcdh_ammo_max",0)
            room:setPlayerMark(player, "mcdh_ammo", 0)
            room:setPlayerMark(player, "@mcdh_ammo", 0)
        end
        if general.ammo then
            local now_ammo = player:getMark("mcdh_ammo")
            lib.mcdh_ammo(player, general.ammo-now_ammo)
        end
    end,
})

Fk:loadTranslationTable {
    ["mcdh_ammo_skill"] = "装填",
    ["ammo_skill"] = "摸牌阶段，你可以少摸任意张牌，装填等量弹药" ..
            "<font color='grey'><small>" ..
            "<br>当一名角色发动带有“<b>装填技(n)</b>”的技能时，将消耗n发弹药。" ..
            "<br>如果弹药小于n，则不能发动该裝填技。" ..
            "<br>若n为“X”，且后续技能描述中没有为X赋值，则角色发动技能时，X为玩家声明的一个任意正整数。" ..
            "</small></font>",
    ["#ammo-Invoke"] = "装填:是否少摸任意牌，装填等量弹药",

}

return ammo